
const { bot } = require("../index")
const axios = require('axios')
const { segment } = require('oicq')
const { info } = require('./myconfig')
const { noPunch } = require('./plugin-picPunch')
const { dianzan } = require('./ThumbUp')
const { tools } = require('./myFn/tools.js')
// 监听群消息
axios.default.timeout = 8000
bot
  .on('message.group', async function (msg) {
    if (info.allow_group.includes(msg.group_id)) {
      //帮助菜单
      if (msg.raw_message === 'help') {
        let helpStr = info.helpFun.map((item, index) => {
          return `${index + 1}.${item}\n`
        })
        let strs = helpStr.join('')
        msg.reply(`目前支持的功能有:\n${strs}其他功能待开发……`, false)
      }
      // 检测是否骂人
      if (info.abuse.say.includes(msg.raw_message)) {
        msg.reply(info.abuse.answer[Math.floor(Math.random() * info.abuse.answer.length)])
      }
      // 命令：今日核酸截图
      if (msg.raw_message === '今日核酸截图') {
        noPunch(msg)
      }
      // 命令：今日图片 xxx
      if (msg.raw_message.includes('今日图片')) {
        // if (msg.raw_message.split(' ').length !== 2) {
        //   msg.reply('格式不正确哦', true)
        // }
        let getMsg = msg.raw_message.trim().split(" ")
        if (getMsg.length > 2) {
          msg.reply('格式不正确~', true)
          return
        }
        axios.get(`http://zx529.xyz:5003/apifqq/duitang/${encodeURI(getMsg[1] ? getMsg[1] : '迪迦奥特曼')}`)
          .then(res => {
            if (res.data.length === 0) {
              msg.reply('没有找到该类型的图片哦~', true)
              return
            }
            let index = tools.getRandomIndex(res.data.length)
            msg.reply(segment.image(res.data[index]))
          }).catch(e => {
            msg.reply(`发生错误了~\n${e}`, false)
          })
      }
      if (msg.raw_message.includes('赞我')) {
        let count = parseInt(msg.raw_message.replace(/[^0-9]/ig, ""))
        let qq = msg.sender.user_id

        if (count > 0 && count <= 10) {
          dianzan(msg, qq, count)
        }
        else {
          dianzan(msg, qq)
        }

      }
      if (msg.raw_message === '名人名言') {
        axios.get('https://v.api.aa1.cn/api/api-wenan-mingrenmingyan/index.php?aa1=json')
          .then(res => {
            let data = res.data['0']['mingrenmingyan']
            msg.reply(data, false)
          })
          .catch(e => {
            console.log(e)
          })
      }
      // ip查询
      if (msg.raw_message.includes('ip' || 'IP')) {
        const ips = msg.raw_message.trim().split(' ')
        if (ips.length !== 2) {
          msg.reply('格式错误,请输入help查看格式', true)
          return
        }
        if (ips[1].split('.').length !== 4) {
          msg.reply('IP格式错误', true)
          return
        }
        axios.get(`http://realip.cc/?ip=${ips[1]}`)
          .then(res => {
            const ipCity = res.data.city ? res.data.city : " "
            const ipProvince = res.data.province ? res.data.province : " "
            const ipCountry = res.data.country ? res.data.country : " "
            const ipContinent = res.data.continent ? res.data.continent : " "
            let repInfo = ipContinent + " " + ipCountry + " " + ipProvince + " " + ipCity
            repInfo = repInfo.trim()
            if (repInfo.length === 0) {
              msg.reply(`${ips[1]}\n可能为内网IP,无法获取`)
              return
            }
            msg.reply(`${ips[1]}的归属地为:\n${repInfo}`, false)

          }).catch(() => {
            msg.reply(`未能获取到${ips[1]}的归属地,请检查格式是否正确`)
          })
      }
      // 娶个老婆
      if (msg.raw_message.includes('老婆')) {
        msg.reply('正在给你找老婆,请稍等……', false)
        axios.get('http://zx529.xyz:5003/apifqq/getWife')
          .then(res => {
            let randPic = res.data[tools.getRandomIndex(res.data.length)]
            msg.reply(segment.flash(randPic), true)
          })
          .catch(e => {
            msg.reply(`今日不可以涩涩哦~\n${e}`)
          })
      }
      if (msg.raw_message === '淘宝买家秀') {
        axios.get('https://api.uomg.com/api/rand.img3?format=json')
          .then(res => {
            msg.reply(segment.image(res.data.imgurl), false)
          }).catch(e => {
            console.log(`找不到了\n${e}`)
          })
      }

      if (msg.raw_message === '网易云热评') {
        axios.get('https://v.api.aa1.cn/api/api-wenan-wangyiyunreping/index.php?aa1=json')
          .then(res => {
            let data = res.data['0']['wangyiyunreping']
            msg.reply(data, false)
          })
          .catch(e => {
            msg.reply(`获取失败\n${e}`, true)
          })
      }
      // 命令：二次元
      if (msg.raw_message.includes('二次元')) {
        // axios.get('https://www.dmoe.cc/random.php?return=json')
        axios.get('https://www.dmoe.cc/random.php?return=json')
          .then(res => {
            console.log(res.data.imgurl)
            console.log(res.data)
            let imgUrl = res.data.imgurl
            msg.reply(segment.image(imgUrl))
          })
          .catch(e => {
            msg.reply(`今日不能涩涩哦~\n${e}`, true)
          })
      }
      if (msg.raw_message === '戳一戳我') {
        msg.member.poke()
      }
      if (msg.raw_message === '来首音乐') {
        axios.get('https://api.uomg.com/api/rand.music?&format=json')
          .then(res => {
            // console.log(typeof (res.data))
            msg.reply(segment.record(`${res.data.data.url}.mp3`))
          })
          .catch(e => {
            msg.reply(`点歌失败哦~\n${e}`, true)
          })
      }
      // 支付宝红包
      if (msg.raw_message === "支付宝红包") {
        msg.reply(segment.image("https://hanruofei-gogogo.oss-cn-chengdu.aliyuncs.com/images/202209301105820.jpg"))
      }
      if (msg.raw_message.includes('点歌')) {
        const result = msg.raw_message.trim().split(' ')
        if (result.length !== 2) {
          msg.reply('格式不对')
          return
        }
        axios.get(`http://cloud-music.pl-fe.cn/search?keywords=${encodeURI(result[1])}`)
          .then(res => {
            const id = res.data.result.songs[0].id
            msg.reply('正在唱,请稍等……', false)
            msg.reply(segment.record(`https://music.163.com/song/media/outer/url?id=${id}.mp3`))
          })
          .catch(e => {
            msg.reply(`点歌失败~请联系管理员修复\n${e}`)
          })
      }
      if (msg.raw_message.includes('qq查询')) {
        let resuArr = msg.raw_message.split(' ')
        if (resuArr.length > 3) {
          msg.reply('参数错误', true)
          return
        }
        let qqNum = resuArr[1]
        axios.get(`https://api.xiaobaibk.com/api/qq/?qq=${qqNum}`)
          .then(res => {

            let result = tools.myRegForQQInfo(res.data)

            msg.reply([
              `QQ昵称: ${result[0]}\nQQ邮箱:${result[1]}`,
              segment.image(result[2])
            ])


          })
        // msg.reply(segment.image(`https://api.xiaobaibk.com/api/qq/?qq=${qqNum}`))
      }
      if (msg.raw_message === '网易云评论') {
        msg.reply('正在找……', true)
        axios.get("http://zx529.xyz:5003/joke")
          .then(res => {
            console.log(res.data)
          })
          .catch(e => { console.log(e) })
      }
      if (msg.raw_message === '神回复') {
        axios.get('https://v.api.aa1.cn/api/api-wenan-shenhuifu/index.php?aa1=json')
          .then(res => {
            let data = res.data
            let rep = data['0']['shenhuifu']
            rep = rep.replace('<br>', '\n')
            msg.reply(rep, false)
          })
          .catch(e => {
            msg.reply(`查找失败,因为${e}`)
          })
      }
      if (msg.raw_message.includes('代发邮件')) {
        let data = msg.raw_message.split(' ')
        console.log(data)
        if (data.length === 4) {
          axios.get(`https://v.api.aa1.cn/api/mail/api.php?address=${encodeURI(data[1])}&name=${encodeURI(data[2])}&certno=${encodeURI(data[3])}`)
            .then(res => {
              let data = JSON.parse(res.data)
            })
            .catch(e => {
              msg.reply(`发送失败,因为${e},`)
            })
          msg.reply('格式正确,正在发送……')
        }
        else {
          msg.reply('格式错误,应为: \n 代发邮件 邮箱 标题 内容')
        }
      }
      if (msg.raw_message.includes('欣欣')) {
        if (msg.sender.user_id === 1643058902) {
          let index = Math.floor(Math.random() * info.love_phrase.length)
          console.log(info.love_phrase[index])
          msg.reply(info.love_phrase[index], false)
        }
        // else {

        // }
      }
      if (msg.raw_message === '讲个笑话') {
        axios.get('https://v.api.aa1.cn/api/api-wenan-gaoxiao/index.php?aa1=json')
          .then(res => {
            let data = res.data['0']
            let rep = data['gaoxiao']
            console.log(rep)
            msg.reply(rep, false)
          })
      }
      // @本人的消息
      if (msg.atme) {
        let text = msg.raw_message.split(' ')[1]

        axios.get(`https://api.ownthink.com/bot?spoken=${encodeURI(text)}`)
          .then((res) => {

            if (res.data.message === 'success') {
              // console.log('请求成功!', res.data.data.info.text)
              let replyInfo = res.data.data.info.text
              msg.reply(replyInfo, false)
            }
            else {
              msg.reply('不理笨蛋~哼哼哼', false)
            }
          })
          .catch((e) => {
            console.log(`不能说话，因为${e}`)
            msg.reply(`不能说话，因为${e}`, false)
          })

      }
    }

  })
const task_qq_group = [675398943, 365263081]

let sendNoPiunch = () => {
  let send_timer = setInterval(() => {
    // let time = new Date()
    // console.log(tools.getMinuteAndSecond())
    bot.sendGroupMsg(task_qq_group[0], `当前时间${tools.getHourAndMinute()}`)
  }, 1000 * 60 * 10)
}
sendNoPiunch()





